//
//  ToggleView.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/29/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit


final class ToggleView: UIView {
    
    // MARK: - Outlets
    
    private lazy var titleLbl: UILabel = {
        let title = UILabel()
        title.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        title.textAlignment = .left
        title.textColor = .darkBlueColor
        return title
    }()
    private lazy var toggleView: UISwitch = {
        let view = UISwitch()
        view.isOn = false
        view.onTintColor = .darkBlueColor
        view.thumbTintColor = .white
        view.tintColor = .mediumGreyBlue
        return view
    }()
    
    // MARK: - Init
    
    init () {
        super.init(frame: .zero)
        self.styleView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func makeToggleView (name: String, isOn: Bool) {
        self.titleLbl.text = name
        self.toggleView.isOn = isOn
    }
    
    func isOn() -> Bool {
        return toggleView.isOn
    }
    
    func changeValue (to value: Bool) {
        self.toggleView.isOn = value
    }
    
    // MARK: - UI
    
    private func styleView () {
        clipsToBounds = true
        layer.cornerRadius = 10
        backgroundColor = .whiteBlueColor
        self.addSubview(titleLbl)
        self.addSubview(toggleView)
        self.addConstraints()
    }
    
    private func addConstraints () {
        toggleView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.trailing.equalToSuperview().inset(16)
            make.width.equalTo(48)
            make.height.equalTo(24)
        }
        titleLbl.snp.makeConstraints { (make) in
            make.leading.equalToSuperview().offset(16)
            make.trailing.equalTo(toggleView.snp.leading).offset(-16)
            make.centerY.equalToSuperview()
            make.height.equalTo(15)
        }
    }
    
}
